package com.noassertions.validation.iscreen;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.iscreen.BaseValidator;
import org.iscreen.ValidatorContext;
import org.springframework.context.ApplicationContext;

import com.noassertions.data.DAOImpl;
import com.noassertions.data.DAOInterface;
import com.noassertions.domain.User;
import com.noassertions.utils.spring.AppContext;

public class EmailExistsValidator extends BaseValidator{
	protected final Log log = LogFactory.getLog(getClass());
	ApplicationContext context = AppContext.getApplicationContext();
	private DAOInterface dao = (DAOImpl) context.getBean("dao");
	
	public void validate(ValidatorContext context, Object beanToValidate) {
		String stringToValidate = getStringValue( beanToValidate );

		User user = dao.getUserByemailAddress(stringToValidate);
		
		if (user.getEmailaddress() != null) {
		    context.reportFailure( getDefaultFailure() );
		}
				
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

}
